package com.hongyuan.sellfitness.ui.person.view_mode;

import android.app.Application;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.mvvmhabitx.bus.event.SingleLiveEvent;
import com.hongyuan.sellfitness.BR;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.base.ItemViewModel;
import com.hongyuan.sellfitness.data.MyRepository;
import com.hongyuan.sellfitness.ui.find.activity.EditPostActivity;
import com.hongyuan.sellfitness.ui.person.activity.AttentionActivity;
import com.hongyuan.sellfitness.ui.find.beans.FindChildBean;
import com.hongyuan.sellfitness.ui.find.view_mode.FindChildItemViewModel;
import com.hongyuan.sellfitness.ui.home.activity.CoachDetailsActivity;
import com.hongyuan.sellfitness.ui.home.activity.CoachLessonActivity;
import com.hongyuan.sellfitness.ui.home.beans.CoachLessonItemBean;
import com.hongyuan.sellfitness.ui.home.view_mode.CoachLessonItemViewModel;
import com.hongyuan.sellfitness.ui.person.activity.EditInformationActivity;
import com.hongyuan.sellfitness.ui.person.activity.FansActivity;
import com.hongyuan.sellfitness.ui.person.beans.IsAttentionBean;
import com.hongyuan.sellfitness.ui.person.beans.UserInfoBean;
import java.util.List;

import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class UserInfoViewModel extends CustomViewModel<MyRepository> {

    //是否为本人主页
    public boolean isMe;
    //用户id
    public String userId;
    //是否关注
    private IsAttentionBean isAttentionBean;

    public ObservableField<UserInfoBean> entity = new ObservableField<>();

    public UserInfoViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        //改变标题
        public SingleLiveEvent<String> changeTitle = new SingleLiveEvent<>();
        //设置底部按钮显示内容
        public SingleLiveEvent<String> changeButtonTv = new SingleLiveEvent<>();
    }

    //给RecyclerView添加ObservableList
    public ObservableList<CoachLessonItemViewModel> lessonList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<CoachLessonItemViewModel> lessonBinding = ItemBinding.of(BR.viewModel, R.layout.item_coach_lesson);

    //给RecyclerView添加ObservableList
    public ObservableList<FindChildItemViewModel> observableList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<FindChildItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_find_child);

    //更多私教课
    public BindingCommand moreClass = new BindingCommand(() -> {
        Bundle bundle = new Bundle();
        bundle.putString("coach_mid",String.valueOf(entity.get().getM_id()));
        startActivity(CoachLessonActivity.class,bundle);
    });

    //去教练主页
    public BindingCommand goCoachDetails = new BindingCommand(() -> {
        Bundle bundle = new Bundle();
        bundle.putString("coach_mid",String.valueOf(entity.get().getM_id()));
        startActivity(CoachDetailsActivity.class,bundle);
    });

    //去我关注的人
    public BindingCommand goAttention = new BindingCommand(() -> {
        if(isMe){
            startActivity(AttentionActivity.class);
        }
    });
    //去我的粉丝
    public BindingCommand goFans = new BindingCommand(() -> {
        if(isMe){
            startActivity(FansActivity.class);
        }
    });

    //修改个人资料
    public BindingCommand goEditInfor = new BindingCommand(() -> {
        if(isMe){
            startActivity(EditInformationActivity.class);
        }
    });

    //关注、取消关注、发布动态
    public BindingCommand goNext = new BindingCommand(() -> {
        if(isMe){
            startActivity(EditPostActivity.class);
        }else{
            if(isAttentionBean.getIs_friend() == 1){
                //取消关注
                setParams("f_mid",userId).setParams("f_type","reduce");
                requestData(Constants.ADD_FRIENT);
            }else{
                //关注
                setParams("f_mid",userId).setParams("f_type","add");
                requestData(Constants.ADD_FRIENT);
            }
        }
    });

    /*
    * 是否显示发布动态按钮
    * */
    public boolean isShowBtn2(){
        if(isMe && userToken.getFindState() != 1){
            return false;
        }else{
            return true;
        }
    }


    /*
    * 请求个人主页信息
    * */
    public void getUserInfo(String userId,String userPhone){
        this.userId = userId;

        clearParams();

        if(userToken.getM_mobile().equals(userPhone)){
            isMe = true;

            //设置按钮显示内容
            uc.changeButtonTv.setValue("发布动态");

            //请求自己发布的帖子
            requestData(Constants.GET_MEMBER_CIRCLE_LIST);
        }else{
            setParams("other_m_id",userId).setParams("f_mid",userId);
            //查询是否已关注当前用户
            requestData(Constants.IS_MY_FRIEND);
            //请求用户主页帖子
            requestData(Constants.GET_OTHER_MBEMBER_CIRCLE_LIST);
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        if(isMe){
            //请求自己的主页信息
            requestData(Constants.CIRCLE_MEMBER_INDEX);
        }else{
            //请求用户主页信息
            requestData(Constants.CIRCLE_OTHER_MEMBER_INDEX);
        }
    }

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.CIRCLE_MEMBER_INDEX || code == Constants.CIRCLE_OTHER_MEMBER_INDEX){
            UserInfoBean infoBean = (UserInfoBean)dataBean;
            entity.set(infoBean);

            if(!isMe){
                //改变标题显示
                uc.changeTitle.setValue("用户主页");
            }

            //判断是否为教练
            if(infoBean.getRole_id() == 2){
                setParams("jl_mid",String.valueOf(infoBean.getM_id()));
                requestData(Constants.GET_COACH_COURSE_PRIVITE);
            }
        }

        if(code == Constants.IS_MY_FRIEND){
            isAttentionBean = (IsAttentionBean)dataBean;
            if(isAttentionBean.getIs_friend() == 1){
                uc.changeButtonTv.setValue("取消关注");
            }else{
                uc.changeButtonTv.setValue("关注Ta");
            }
        }

        if(code == Constants.GET_COACH_COURSE_PRIVITE){
            List<CoachLessonItemBean.ListBean> list = ((CoachLessonItemBean)dataBean).getList();
            if(list != null && list.size() > 0){

                for(int i = 0 ; i < list.size() ; i++){
                    CoachLessonItemViewModel itemViewModel = new CoachLessonItemViewModel(this,list.get(i),i,list.size() - 1);
                    lessonList.add(itemViewModel);
                }
            }
        }

        if(code == Constants.GET_MEMBER_CIRCLE_LIST || code == Constants.GET_OTHER_MBEMBER_CIRCLE_LIST){
            List<FindChildBean.ListBean> mList = ((FindChildBean)dataBean).getList();
            //清除旧数据
            if(curPage == FIRST_PAGE){
                observableList.clear();
            }

            if(mList != null && mList.size() > 0){
                for(FindChildBean.ListBean bean : mList){
                    FindChildItemViewModel itemViewModel = new FindChildItemViewModel(this,bean);
                    itemViewModel.isHomePage = true;
                    observableList.add(itemViewModel);
                }
            }

            if(observableList.size() > 0){
                if(mList == null || mList.size() == 0){
                    getUC().getFinishLoadMoreData().call();
                }
            }else{

            }
        }

        if(code == Constants.ADD_CIRCLE_PRAISE){
            if(itemViewModel != null){
                itemViewModel.entity.get().setIs_praise(1);
                itemViewModel.entity.get().setPraise_num(itemViewModel.entity.get().getPraise_num() + 1);
                itemViewModel.entity.notifyChange();
            }
        }

        if(code == Constants.CANCEL_CIRCLE_PRAISE){
            if(itemViewModel != null){
                itemViewModel.entity.get().setIs_praise(0);
                itemViewModel.entity.get().setPraise_num(itemViewModel.entity.get().getPraise_num() - 1);
                itemViewModel.entity.notifyChange();
            }
        }

        if(code == Constants.ADD_FRIENT){
            if(isAttentionBean.getIs_friend() == 1){
                isAttentionBean.setIs_friend(0);
                uc.changeButtonTv.setValue("关注Ta");
            }else{
                isAttentionBean.setIs_friend(1);
                uc.changeButtonTv.setValue("取消关注");
            }
        }
    }

    //操作的子项
    private FindChildItemViewModel itemViewModel;

    @Override
    public void itemClick(ItemViewModel itemVM) {
        super.itemClick(itemVM);

        itemViewModel = (FindChildItemViewModel) itemVM;

    }
}
